package com.fyt.algorithm.Leecode.day1;

import java.util.*;

public class TheSumOfTwoNumbers {


    public int[] twoSum(int[] nums, int target) {
        Map<Integer,Integer> map=new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int complement=target-nums[i];

            if (map.containsKey(complement)){
                return new int[] {map.get(complement),i};
            }
            map.put(complement,i);
        }
       throw new IllegalArgumentException();
    }

    public static void main(String[] args) {
        TheSumOfTwoNumbers solution = new TheSumOfTwoNumbers();
        int[] nums = {3,2,4};
        int target = 6;

        try {
            int[] res = solution.twoSum(nums, target);
            System.out.println("找到的两个数的索引为: " + res[0] + " 和 " + res[1]);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }
}
